package com.cy.ppj.web.front.user;

import com.cy.ppj.model.ao.user.BindBankCardAO;
import com.cy.ppj.model.ao.user.CertificationAO;
import com.cy.ppj.model.ao.user.UpdatePasswordAO;
import com.cy.ppj.model.domian.user.UserInfo;
import com.cy.ppj.model.enums.SmsType;
import com.cy.ppj.model.query.ChannelUserBySalesQuery;
import com.cy.ppj.model.vo.ChannelUserBySalesVO;
import com.cy.ppj.service.account.BankCardService;
import com.cy.ppj.service.account.CertificationService;
import com.cy.ppj.service.account.UserInfoService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cy.ppj.comm.utils.CommonUtils;
import com.cy.ppj.model.PageBean;
import com.cy.ppj.model.Result;
import com.cy.ppj.service.user.UserService;
import com.cy.ppj.web.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.Map;

/**
 * @description: 前台-我的页面
 * @author: Ocean
 * @create: 2018-07-19 19:41
 **/
@Slf4j
@RestController
@RequestMapping("/user")
@Api(tags = "前台-我的页面")
public class UserController extends BaseController{

	@Autowired
	private UserService userService;
	@Autowired
	private UserInfoService userInfoService;
	@Autowired
	private BankCardService bankCardService;
	@Autowired
	private CertificationService certificationService;

	@ApiOperation("实名验证")
	@PostMapping("/certification")
	public Result certification(@Validated CertificationAO certificationAO, BindingResult bindingResult) {

		this.checkError(bindingResult);

		Long userId = this.getUser().getUserId();

		UserInfo userInfo = new UserInfo();
		userInfo.setIdNo(certificationAO.getIdNo());
		int count = userInfoService.queryUserInfoCount(userInfo);
		if (count > 0) {
			return Result.fail("该身份证号已注册");
		}

		userInfo = new UserInfo();
		userInfo.setUserId(userId);
		userInfo = userInfoService.queryUserInfo(userInfo);
		if (userInfo != null) {
			return Result.fail("用户已完成实名验证");
		}

		Result<UserInfo> result = certificationService.certification(certificationAO.getName(), certificationAO.getIdNo());
		if (!result.isOk()) {
			return result;
		}

		userInfo = result.getValue();
		userInfo.setUserId(userId);
		userInfo.setCityId(certificationAO.getCityId());
		userInfoService.insertUserInfo(userInfo);

		return ok();
	}
	
	@ApiOperation("绑定业务员")
	@PostMapping("/bind/salesman")
	public Result bindSalesman(@ApiParam(value = "业务员手机号码", required = true) @RequestParam String mobile) {
		return userService.bindSalesman(mobile, this.getUser().getUserId());
	}

	@ApiOperation("查询银行卡信息")
	@GetMapping("/query/bank/card")
	public Result bindBackCard(@RequestParam @ApiParam(value = "银行卡号") String bankCard) {
		return bankCardService.queryBankCard(bankCard);
	}

	@ApiOperation("绑定银行卡")
	@PostMapping("/bind/bank/card")
	public Result bindBackCard(@Validated BindBankCardAO bindBankCardAO, BindingResult bindingResult) {

		this.checkError(bindingResult);
		this.checkSmsCode(bindBankCardAO.getBankMobile(), bindBankCardAO.getSmsCode(), SmsType.BINDCARD);

		try {

			Long userId = this.getUser().getUserId();
			UserInfo dbUserInfo = new UserInfo();
			dbUserInfo.setUserId(userId);
			dbUserInfo = userInfoService.queryUserInfo(dbUserInfo);
			if (dbUserInfo != null) {
				bindBankCardAO.setIdNo(dbUserInfo.getIdNo());
				bindBankCardAO.setRealName(dbUserInfo.getRealName());
			}

			UserInfo userInfo = new UserInfo();
			BeanUtils.copyProperties(bindBankCardAO, userInfo);

			//校验四要素
			Result result = bankCardService.verifyBankCard(userInfo);
			if (!result.isOk()) {
				return result;
			}

			//查询银行卡
			result = bankCardService.queryBankCard(userInfo.getBankCard());
			if (!result.isOk()) {
				return result;
			}
			Map<String, Object> queryMap = (Map) result.getValue();
			String bankNo = queryMap.get("banknum").toString();

			userInfo.setBankNo(bankNo);
			userInfo.setSex(CommonUtils.getSexByIdNo(userInfo.getIdNo()));
			userInfo.setUserId(userId);
			userInfo.setCityId(bindBankCardAO.getCityId());
			userInfoService.insertUserInfo(userInfo);
		} catch (Exception e) {
			log.error("bindBackCard error:", e);
			return Result.fail("绑定失败");
		}

		return ok();
	}
	
	@ApiOperation("修改密码")
	@PostMapping("/update/password")
	public Result updatePassword(@Validated UpdatePasswordAO updatePasswordAO, BindingResult bindingResult) {
		this.checkError(bindingResult);
		userService.updatePassword(this.getUser().getUserId(), updatePasswordAO.getOldPassword(), updatePasswordAO.getNewPassword());
		return ok();
	}
	
	@ApiOperation("获取渠道员列表")
	@GetMapping("/get/channel/list")
	public PageBean<ChannelUserBySalesVO> getChannelList(ChannelUserBySalesQuery channelUserBySalesQuery) {
		channelUserBySalesQuery.setUserId(this.getUser().getUserId());
		return userService.queryChannelUserBySales(channelUserBySalesQuery);
	}
}
